package com.yylending.las.personal.version;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.personal.Version;

import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by HenryZhang on 2016/11/23.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class VersionModel extends Model {

	private static final String TAG = "VersionModel";

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		return service.update(Global.LOGIN_TOKEN, (Update) params);
	}

	@Override
	public void onNext(Object entity) {
		CLog.i(TAG, "onNext: " + entity.toString());
		UpdateResult result = cast(entity);
		EventAgent.post(result);
	}

	static class Update {
		public String version = Global.VERSION;
	}

	private interface Service {
		/**
		 * 版本接口
		 */
		@POST("server/version/getVersion")
		Observable<UpdateResult> update(@Header("token") String token, @Body Update update);
	}

	static class UpdateResult extends BaseResultEntity<Version> {
	}

}
